package vectorspacemodel;
import java.util.*;
import java.io.*;

public class Indexing 
{
 ArrayList doc_al=new ArrayList();
 ArrayList doc_paths=new ArrayList();
 TreeSet unique_words=new TreeSet();
 TreeMap index=new TreeMap();
 int no_of_doc;
    
 void get_Documents_From_Directory(String dir_name)throws IOException
 {
  File directory = new File(dir_name);      
  File[] list_of_files = directory.listFiles();  
  for (int j = 0; j < list_of_files.length; j++)
    {
    if(list_of_files[j].isDirectory())
        {
        get_Documents_From_Directory(list_of_files[j].getAbsolutePath());
        }
    if(list_of_files[j].isFile())
        {
        File path=list_of_files[j];
        FileReader fr = new FileReader(path);
        BufferedReader br = new BufferedReader(fr);
        String doc_content = "";
        while (br.ready()) 
            {
            doc_content += br.readLine() + "\n";
            }
        doc_al.add(doc_content);
        doc_paths.add(list_of_files[j]);
        get_Unique_Words(doc_content);
        }
     } 
 }
       
 void get_Unique_Words(String doc)
 {
  StringTokenizer st = new StringTokenizer(doc);
  String word;
  while(st.hasMoreTokens())
    {
    word = st.nextToken();
    if(unique_words.contains(word))
        {
         continue;
        }
  unique_words.add(word);
    }
 }
 
 void create_index()
 {
  Iterator itr=unique_words.iterator();
  while(itr.hasNext())
    {
    String path="";
    String dic_word=(String)itr.next();
    for(int i=0;i<doc_al.size();i++)
        {
        String content=(String)doc_al.get(i);
        StringTokenizer st= new StringTokenizer(content);
        String word;
        while(st.hasMoreTokens())
            {
            word=st.nextToken();
            if(dic_word.equals(word))
                {
                path += doc_paths.get(i)+"\n";
                break;
                }
            }
        }
    index.put(dic_word, path);
    }
 }
  
 void create_index_obj()
 {
  try
    {
    Index_obj i_obj=new Index_obj(index);
    FileOutputStream fos=new FileOutputStream("serial");
    ObjectOutputStream oos=new ObjectOutputStream(fos);
    oos.writeObject(i_obj);
    oos.flush();
    oos.close(); 
    }
  catch(Exception e)
    {
    System.out.println("Exception during serialization: "+e);
    }    
 }
 
 /*void display()
 {
  Iterator itr=unique_words.iterator();
  while(itr.hasNext())
    {
    System.out.println(itr.next());
    }
  Set set=index.entrySet();
  itr=set.iterator();
  while(itr.hasNext())
    {
    Map.Entry me=(Map.Entry) itr.next();
    System.out.print(me.getKey()+"\n");
    System.out.println(me.getValue());
    }
 }*/
 
 /*void read_index_obj()
 {
  try
    {
    Index_obj io;
    FileInputStream fis=new FileInputStream("serial");
    ObjectInputStream ois=new ObjectInputStream(fis);
    io=(Index_obj)ois.readObject();
    ois.close();
    Set set=io.i_obj.entrySet();
    Iterator itr=set.iterator();
    while(itr.hasNext())
        {   
        Map.Entry me=(Map.Entry) itr.next();
        System.out.print(me.getKey()+"\n");
        System.out.println(me.getValue());
        }
    }
  catch(Exception e)
    {
    System.out.println("Exception during deserialization: "+e);
    }
 }*/
}